#include "sdk.h"
#include "projectdepsdlg.h"

BEGIN_EVENT_TABLE( ProjectDepsDlg, wxScrollingDialog )
  EVT_CHOICE( XRCID( "cmbProject" ), ProjectDepsDlg::OnProjectChange )
END_EVENT_TABLE()

ProjectDepsDlg::ProjectDepsDlg( wxWindow* parent, M_Code_Project* sel ) : m_LastSel( -1 ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置项目对象.xrc" );
  if( !xml_res->Load( resPath ) )  {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( F( _T( "文件丢失: %s " ), resPath.wx_str() ) );
    return;
  }
  if( !xml_res->LoadObject( this, parent, _T( "dlgConfigureProjectDeps" ), _T( "wxScrollingDialog" ) ) ) {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( _T( "资源丢失" ) );
    return;
  }
  xml_File_lock = true;
  XRCCTRL( *this, "wxID_CANCEL", wxButton )->SetDefault();
  wxChoice* cmb = XRCCTRL( *this, "cmbProject", wxChoice );
  int idx = 0;
  ProjectsArray* mainarr = Manager::Get()->GetProjectManager()->GetProjects();
  for( size_t i = 0; i < mainarr->GetCount(); ++i ) {
    M_Code_Project* prj = mainarr->Item( i );
    cmb->Append( prj->GetTitle(), prj );
    if( prj == sel ) {
      idx = i;
    }
  }
  cmb->SetSelection( idx );
  m_LastSel = idx;
  FillList();
  Fit();
}

ProjectDepsDlg::~ProjectDepsDlg() {
}

bool ProjectDepsDlg::SaveList() {
  wxChoice* cmb = XRCCTRL( *this, "cmbProject", wxChoice );
  wxCheckListBox* lst = XRCCTRL( *this, "lstDeps", wxCheckListBox );
  if( m_LastSel == -1 ) {
    return true;
  }
  M_Code_Project* thisprj = static_cast<M_Code_Project*>( cmb->GetClientData( m_LastSel ) );
  if( !thisprj ) {
    return true;
  }
  Manager::Get()->GetProjectManager()->ClearProjectDependencies( thisprj );
  for( size_t i = 0; i < lst->GetCount(); ++i ) {
    if( !lst->IsChecked( i ) ) {
      continue;
    }
    M_Code_Project* prj = nullptr;
    ProjectsArray* mainarr = Manager::Get()->GetProjectManager()->GetProjects();
    for( size_t x = 0; x < mainarr->GetCount(); ++x ) {
      if( mainarr->Item( x )->GetTitle() == lst->GetString( i ) ) {
        prj = mainarr->Item( x );
        break;
      }
    }
    if( !prj ) {
      continue;
    }
    if( !Manager::Get()->GetProjectManager()->AddProjectDependency( thisprj, prj ) ) {
      InfoWindow::Tip( F( _T( "无法添加项目 '%s' 作为对 '%s' 因为这会导致循环依赖关系错误 " ), thisprj->GetTitle().c_str(),
                          prj->GetTitle().c_str() ), _T( "错误" ), 3500 );
      return false;
    }
  }
  return true;
}

void ProjectDepsDlg::FillList() {
  wxChoice* cmb = XRCCTRL( *this, "cmbProject", wxChoice );
  wxCheckListBox* lst = XRCCTRL( *this, "lstDeps", wxCheckListBox );
  int idx = cmb->GetSelection();
  if( m_LastSel != idx && m_LastSel != -1 ) {
    SaveList();
  }
  m_LastSel = idx;
  if( idx == -1 ) {
    return;
  }
  M_Code_Project* thisprj = static_cast<M_Code_Project*>( cmb->GetClientData( idx ) );
  if( !thisprj ) {
    return;
  }
  const ProjectsArray* arr = Manager::Get()->GetProjectManager()->GetDependenciesForProject( thisprj );
  lst->Clear();
  ProjectsArray* mainarr = Manager::Get()->GetProjectManager()->GetProjects();
  for( size_t i = 0; i < mainarr->GetCount(); ++i ) {
    M_Code_Project* prj = mainarr->Item( i );
    if( prj == thisprj ) {
      continue;
    }
    lst->Append( prj->GetTitle() );
    lst->Check( lst->GetCount() - 1, arr && arr->Index( prj ) != wxNOT_FOUND );
  }
}

void ProjectDepsDlg::OnProjectChange( M_Code_unused wxCommandEvent& event ) {
  FillList();
}

void ProjectDepsDlg::EndModal( int retCode ) {
  if( SaveList() ) {
    return wxScrollingDialog::EndModal( retCode );
  }
}
